package org.shop.weixin.util;


import org.apache.commons.lang.StringUtils;

public class EmojiFilterUtils {

    /** 
     * 将emoji表情替换成* 
     *  
     * @param source 
     * @return 过滤后的字符串 
     */
    public static String filterEmoji(String source) {
        if (StringUtils.isNotBlank(source)) {
            //这是之前写的正则，有问题
            //return source.replaceAll("[\\ud800\\udc00-\\udbff\\udfff\\ud800-\\udfff]", "");
            //用下面这行就没问题了
            return source.replaceAll(
                "[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]", "*");

        } else {
            return source;
        }
    }

    public static void main(String[] arg) {
        try {
            //金灵 Fairy🌻1
            String text = "\u91d1\u7075\u0020\u0046\u0061\u0069\u0072\u0079\ud83c\udf3b\u0031";
            System.out.println(text);
            System.out.println(text.length());
            System.out.println(filterEmoji(text));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}